/*
 * Default callback "object" used for AJAX queries below. Used by a number of methods.
 */
var callback = {
   /* "Class" variables! Ha, it works like normal classes! (to a point) */
   form:      null,
   submitBtn: null,
   json:      null,
   response:  null,
   
   setForm: function (form) {
      this.form = form;
      this.submitBtn = YAHOO.util.Selector.query('input[type="submit"]', this.form)[0];
      YAHOO.util.Connect.setForm(this.form);
   },
   
   customevents: {
      /* Change submit button to Loading and disable it */
      onStart: function (eventType, args) {
         // "this" prefers to customevents level. Too bad there's no easier way to get to the top level
         callback.start();
         if ( callback.submitBtn != null ) {
            //callback.submitBtn.oldvalue = callback.submitBtn.value; // Store the old value
            //callback.submitBtn.value = "Saving...."; // Change it.
            callback.submitBtn.disabled = true; // Disable it.
         }
         container.wait.show();
      }, 
      
      /* On completion, regardless of success, reset the submit button and enable it */
      onComplete: function (eventType, args) {
         callback.complete();
         if ( callback.submitBtn != null ) {
            //callback.submitBtn.value = callback.submitBtn.oldvalue; // Put the old value back
            callback.submitBtn.disabled = false; // Enable it.
         }
      }
   },
   
   // Incase an extending class needs to add more to the above.
   complete: function() {},
   start: function() {},
   
   /* Successful response is always in JSON. Parse it out and varify
    * it was really successful.
    */
   success: function(o) {
      this.response = o;
      this.json = YAHOO.lang.JSON.parse(o.responseText);
      if ( this.json.success == true ) {
         this.processSuccess(this.json);
      } else {
         this.processFailure();
      }
      container.wait.hide();
   },
   
   /* Method to call with succesful response. Override this one! */
   processSuccess: function(json) {
      alert("Override Me!\n\nProcess Success: " + json);
   },
   
   /* Failure responsys. Might change later on. */
   failure: function(o) {
      div = document.createElement('div');
      span = document.createElement('span');
      span.innerText = 'Oops! Something didn\'t run right. Please try again. We have been notifed.';
      div.appendChild(span);
      div.className = 'error';
      
      YAHOO.log("Failure: " + o.statusText + "\nResponseCode: "+ o.status + "\nReturn Headers: " + o.getAllResponseHeaders + "\nResponse: " + o.responseText, 'error');
      return false;
   },
         
   processFailure: function () {
      YAHOO.log("Big failure!\n\n  Error ID: " + this.json.errnum + "\n  Error Message: " + this.json.errmsg, "error");
      alert(this.json.errmsg); 
   },
   
   timeout: 2000
}


function check_blur ( input, default_txt ) {
   if ( input.value == "" ) {
      input.style.setProperty('color', '#999', null);
      input.value = default_txt;
   }
}

function check_focus ( input, default_txt ) {
   if ( input.value == default_txt ) {
      input.style.setProperty('color', '#000', null);
      input.value = "";
   }
}
